package client;

import gui.Search_GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class CatalogManaging extends JFrame implements ActionListener 
{
	public Search_Control catalogManag = null;
	public EditItem editAnItem = null;
	public static JTable table;
	public JScrollPane ScrollPane = null;
	public JButton Back = null;
	public JButton search = null;
	public JButton logOut = null;
    public String[] columnNames = {"Name", "Image", "Price", "Type", "Id", "Catalog", "Sale", "Edit Item"};
    public Object[][] data = null;
    public JButton editItem = new JButton();
    public JRadioButton inCatalog = null;
    public JRadioButton onSale = null;
    public JRadioButton notOnSale = null;
    public JRadioButton allItems = null;
	public JRadioButton notInCatalog = null;
	public ButtonGroup btnGroup = null;
	public ButtonGroup btnGroupSale = null;
    public static ArrayList<Item> list = null; 
    public static setImage img = null;
    int width = (int)Main.dim.getWidth();
	int height = (int)Main.dim.getHeight();
	
	public CatalogManaging(String show)
	{
		Main.main.whatToShow = show;
		data = getDataFromDB(show);
		
		img = new setImage("catalogManaging"); 
        img.setLayout(null);
		
        this.setTitle("Catalog Managing");
		this.setPreferredSize(new Dimension(width-100, height-200));
		this.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
		this.setResizable(false);
	    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        
	    inCatalog = new JRadioButton("Items In Catalog");
	    inCatalog.setBackground(new Color(221,234,180));
	  	inCatalog.setBounds(10, 62, 180, 40);
	  	inCatalog.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
	  	inCatalog.addActionListener(this);
	  	img.add(inCatalog);
		
		notInCatalog = new JRadioButton("Items Not In Catalog");
		notInCatalog.setBounds(210, 62, 200, 40);
		notInCatalog.setBackground(new Color(221,234,180));
		notInCatalog.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		notInCatalog.addActionListener(this);
		img.add(notInCatalog);
		
		allItems = new JRadioButton("All Items");
		allItems.setBounds(420, 62, 150, 40);
		allItems.setBackground(new Color(221,234,180));
		allItems.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		allItems.addActionListener(this);
		img.add(allItems);
		
		onSale = new JRadioButton("On Sale");
		onSale.setBounds(10, 102, 180, 40);
		onSale.setBackground(new Color(221,234,180));
		onSale.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		onSale.addActionListener(this);
		img.add(onSale);
		
		notOnSale = new JRadioButton("Not On Sale");
		notOnSale.setBounds(210, 102, 200, 40);
		notOnSale.setBackground(new Color(221,234,180));
		notOnSale.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		notOnSale.addActionListener(this);
		img.add(notOnSale);
		
		btnGroupSale=new ButtonGroup();
		btnGroupSale.add(onSale);
		btnGroupSale.add(notOnSale);
		
		btnGroup=new ButtonGroup();
		btnGroup.add(inCatalog);
		btnGroup.add(notInCatalog);
		btnGroup.add(allItems);
		
        Back = new JButton("Back");
		Back.setBounds((int)this.getPreferredSize().getWidth()-115,10,100,40);
		Back.addActionListener(this);
		img.add(Back);
		
		search= new JButton("Search");
		search.setBounds((int)this.getPreferredSize().getWidth()-115,62,100,40);
		search.addActionListener(this);
		img.add(search);
		
		logOut = new JButton("LogOut");
		logOut.setBounds(10,10,100,40);
		logOut.addActionListener(this);
		img.add(logOut);
		
		table = new JTable(new MyTableModel(data, columnNames, "catalogManaging"));
        table.setPreferredScrollableViewportSize(new Dimension(500, 300));
        table.setFillsViewportHeight(true);
        table.setRowHeight(100);
        
        table.getColumnModel().getColumn(2).setPreferredWidth(1);
        table.getColumnModel().getColumn(3).setPreferredWidth(1);
        table.getColumnModel().getColumn(4).setPreferredWidth(1);
        table.setIntercellSpacing(new Dimension(30,30));
        table.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
		table.getTableHeader().setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,20));
		table.getTableHeader().setForeground(new Color(92,64,51));
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        
        final DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();  
        dtcr.setHorizontalTextPosition(JLabel.CENTER);  
        table.getColumnModel().getColumn(2).setCellRenderer(dtcr);
        table.getColumnModel().getColumn(4).setCellRenderer(dtcr);
        table.setBackground(new Color(253,245,230));
        
        editItem.addActionListener(this);
    
        table.getColumnModel().getColumn(7).setCellRenderer(new ButtonRenderer());
        table.getColumnModel().getColumn(7).setCellEditor(new ButtonEditor(new JCheckBox()));
        
        if(show.equals("inCatalog"))
			inCatalog.setSelected(true);
		else 
			if(show.equals("notInCatalog"))
				notInCatalog.setSelected(true);
		else 
			if(show.equals("allItems"))
				allItems.setSelected(true);
       
        if(show.equals("onSale"))
        	onSale.setSelected(true);
        else 
			if(show.equals("notOnSale"))
				notOnSale.setSelected(true);
        
        ScrollPane = new JScrollPane(table);
		ScrollPane.setBounds(10, 150, (int)this.getPreferredSize().getWidth()-25, (int)this.getPreferredSize().getHeight()-200);
		img.add(ScrollPane);
		
		this.add(img);
		this.pack();
		this.setVisible(true);
	}
	
	public Object[][] getDataFromDB(String show)
	{
		if(!show.equals("Search"))
			catalogManag = new Search_Control("-1", "-1", "-1", Search_GUI.colors, Search_GUI.types, show);
		else
			catalogManag = MainScreen.src.search;
		
		list = Search_Control.l;
		data = new Object[list.size()][8];
		
		for (int i=0; i < list.size(); i++)
		{        
			data[i][0] = list.get(i).getName();
			data[i][1] = new ImageIcon(list.get(i).getMyIconArray());
			data[i][2] = list.get(i).getPrice();
			data[i][3] = list.get(i).getItemTypNname();
			data[i][4] = list.get(i).getId(); 
			
			if(list.get(i).inCatalog == 1)
				data[i][5] = "In Catalog";
			else
				data[i][5] = "Not In Catalog";
			
			if(list.get(i).discount == 1)
				data[i][6] = "On Sale";
			else
				data[i][6] = "Not On Sale";
		}
		return data;
	}
	
	public void actionPerformed(ActionEvent e) 
	{		
		if(e.getSource() == editItem)
		{
			this.setEnabled(false);
			editAnItem = new EditItem(data, table.getSelectedRow(), list);
		}
		
		if(e.getSource() == inCatalog)
		{
			Main.main.whatToShow = "inCatalog";
			Main.main.catalogManaging.doClick();
			this.dispose();
		}
		
		if(e.getSource() == notInCatalog)
		{
			Main.main.whatToShow = "notInCatalog";
			Main.main.catalogManaging.doClick();
			this.dispose();
		}
		
		if(e.getSource() == allItems)
		{
			Main.main.whatToShow = "allItems";
			Main.main.catalogManaging.doClick();
			this.dispose();
		}
		
		if(e.getSource() == onSale)
		{
			Main.main.whatToShow = "onSale";
			Main.main.catalogManaging.doClick();
			this.dispose();
		}
		
		if(e.getSource() == notOnSale)
		{
			Main.main.whatToShow = "notOnSale";
			Main.main.catalogManaging.doClick();
			this.dispose();
		}
		
		if(e.getSource() == search)
		{
			Main.main.callFrom = "CatalogManaging";
			Main.main.search.doClick();
			this.setEnabled(false);
		}
		
		if(e.getSource() == Back)
		{
			Main.main.whatToShow = "allItems";
			this.setVisible(false);
			Main.main.setEnabled(true);
			Main.main.setVisible(true);
		}
		
		if(e.getSource() == logOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
	}
	
	class ButtonRenderer extends JButton implements TableCellRenderer 
    {
    	public ButtonRenderer() 
    	{setOpaque(true);}
    	public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column)
    	{  
    		setText("Edit Item");
    		return this;
    	}
    }
    
    class ButtonEditor extends DefaultCellEditor 
    {
    	private String label;
    	public ButtonEditor(JCheckBox checkBox) 
    	{
    		super(checkBox);
    	}
    	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 
    	{
        		label = "Edit Item";
        		editItem.setText(label);
        		return editItem;
    	}
    	public Object getCellEditorValue() 
    	{
    		return new String(label);
    	}
    }
}
